// pages/publish/publish.js
var post_img = [];
Page({

  data: {
    plate: [],
    user_id: '',
    post_img: [],
    plate_id: '',
    post_title: "",
    post_text: ''
  },

  onLoad: function (options) {

  },
  onShow: function () {
    var value = wx.getStorageSync('user_id');
    if (value) {
      this.getPlateList();
      this.setData({
        user_id: value
      })
    } else {
      app.login()
        .then(app.doLogin)
        .then(app.checkAuth('scope.userInfo'))
        .then(app.getUserInfo)
        .then(app.setUserData)
        .then(this.getPlateList)
        .catch(app.toLoad)
    }
  },
  // 获取板块列表
  getPlateList: function () {
    let that = this;
    return new Promise((resolve,reject)=>{
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/get_plate_list',
        data: {},
        success(res) {
          console.log(res.data.data);
          that.setData({
            plate: res.data.data.plate
          })
          resolve(res);
        }
      })
    })
  },
  bindPickerChange: function (e) {
    console.log(e.detail.value)
    var index = e.detail.value;
    this.setData({
      index: e.detail.value,
      plate_id: this.data.plate[index].plate_id
    })
  },
  bindKeyInput: function (e) {
    // console.log(e.detail.value)
    this.setData({
      post_title: e.detail.value
    })
  },
  bindTextAreaBlur: function (e) {
    console.log(e.detail.value)
    this.setData({
      post_text: e.detail.value
    })
  },
  // 发布帖子
  setPostData: function () {
    let that = this;
    var post_img_str = that.data.post_img.join(',');
    if (that.data.plate_id == "") {
      wx.showToast({
        title: '请选择板块！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.post_title == "") {
      wx.showToast({
        title: '帖子标题不能为空！',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.post_text == "") {
      wx.showToast({
        title: '帖子内容不能为空！',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/set_post_data',
        data: {
          plate_id: that.data.plate_id,
          post_title: that.data.post_title,
          user_id: that.data.user_id,
          post_text: that.data.post_text,
          post_img: post_img_str
        },
        success(res) {
          console.log(res)
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      })
    }
  },
  upload: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: 'https://rtutrip.aihuodong.top/index/index/post_picture_upload',
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = JSON.parse(res.data);
            if (data.ret == 200) {
              post_img.push(data.data.path);
            }
            that.setData({
              post_img: post_img
            })
            console.log(that.data.post_img)
          }
        })
      }
    })
  }

})